<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../../lib/innerIframe-include-ext.js"></script>
<script type="text/javascript" src="../../lib/loader-theme.js"></script>
<script type="text/javascript" src="../../lib/ext/locale/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="../../lib/ext/plugins/DataTip.js"></script>
<script type="text/javascript" src="../../js/moment.min.js"></script>
<title>产品线管理</title>
</head>
<script type="text/javascript">
Ext.require(['*']);
Ext.onReady(function(){
    Ext.QuickTips.init();
    Ext.define('webuser', {
        extend: 'Ext.data.Model',
        fields: [
            'id','app_id','app_name','app_type','status','auth_type','access_key','meta','create_at','update_at'
        ],
        idProperty: 'id'
    });
    
    var store = Ext.create('Ext.data.Store', {
        pageSize: 20,
        model: 'webuser',
        remoteSort: true,
        proxy: {
            type: 'ajax',
            method: 'GET',
            url: '/apps/list',
            reader: {
                type: 'json',
                root: 'data',
                totalProperty: 'count'
            },
            simpleSortMode: true
        },
        sorters: [
            {
                property: 'update_at'
            }
        ]
    });
    
    var grid = Ext.create('Ext.grid.Panel', {
        id:'my_grid',
        store: store,
        loadMask: true,
        region: 'center',
        selModel: Ext.create('Ext.selection.CheckboxModel'),
        columns: [
            {
                text: "应用ID",
                dataIndex: 'id',
                hidden: true
            },{
                text:'应用名称',
                dataIndex:'app_name'
            },{
                text: "应用编码",
                dataIndex: 'app_id'
            },{
                text:'应用类型',
                dataIndex:'app_type',
                renderer: function(val) {
                    if(val == '0')
                        return "普通应用";
                    else if(val == '1')
                        return "管理应用";
                    else
                        return val;
                }
            },{
                text: '验证类型',
                dataIndex: 'auth_type',
                renderer:function(val) {
                    if(val == '0')
                        return "无需验证";
                    else if (val == '1')
                        return "口令验证";
                    else
                        return val;
                }
            },{
                text: '访问口令',
                dataIndex: 'access_key'
            },{
                text: '应用信息',
                dataIndex: 'meta'
            },{
                text: '创建日期',
                dataIndex: 'create_at',
                renderer: function(val) {
                    return moment(val).format('YYYY-MM-DD');
                }
            },{
                text: '更新日期',
                dataIndex: 'update_at',
                renderer: function(val) {
                    return moment(val).format('YYYY-MM-DD');
                }
            },{
                text:'状态',
                dataIndex:'status',
                renderer:function(val) {
                    if(val==0 || val=='0')
                        return "<span style='color:green;'>正常</span>";
                    else if(val == '1')
                        return "<span style='color:red;'>禁止</span>";
                    else
                        return val;
                }
            }
        ],
        bbar: Ext.create('Ext.PagingToolbar', {
            store: store,
            displayInfo: true,
            displayMsg: '当前显示  {0} - {1} 共  {2} 条',
            emptyMsg: "没有数据显示！",
            beforePageText: "页码",
            afterPageText: "共 {0} 页",
            firstText:"首页",
            lastText:"末页",
            nextText:"下一页",
            prevText:"上一页",
            refreshText:"刷新"
        }),
        tbar: [
            {
                icon : '../../img/add.png',
                text: "添  加",
                disable:false,
                handler: addHandler
            },{
                icon : '../../img/edit.png',
                text: "修  改",
                disable:false,
                handler: updHandler
            },{
                icon : '../../img/delete.png',
                text : '删  除',
                disable:false,
                handler : delHandler
            }]
    });
    store.loadPage(1);
    
    var viewport = Ext.create('Ext.Viewport', {
        id: 'border-example',
        layout: 'fit',
        items: grid
    });
    
    var addWin;
    var form;
    form = new Ext.FormPanel({
        id: 'myForm',
        method:'POST',
        frame:true,
        style:'border-width:0 0 0 0;',
        bodyBorder:false,
        bodyPadding: '0 40 0 0',
        layout: 'form',
        plugins: {
            ptype: 'datatip'
        },
        defaults:{
            labelAlign:'right'
        },
        items: [
            {
                xtype:'hidden',
                name:'id'
            },{
                xtype: 'label',
                style: 'paddingLeft:45px;',
                html: '配置参考：<a href="http://wiki.gf.com.cn/pages/viewpage.action?pageId=29500168" target="_blank">http://wiki.gf.com.cn/pages/viewpage.action?pageId=29500168</a>'
            },{
                xtype:'textfield',
                fieldLabel:'应用编码',
                name:'app_id',
                labelAlign: 'right',
                maxLength:35,
                allowBlank:false,
                tooltip:'请输入应用ID/编码'
            },{
                xtype:'textfield',
                fieldLabel:'应用名称',
                name:'app_name',
                labelAlign: 'right',
                maxLength:35,
                allowBlank:false,
                tooltip:'请输入应用名称'
            },{
                xtype: 'radiogroup',
                fieldLabel: '设置状态',
                vertical: true,
                id:'status',
                items: [
                    {
                        boxLabel: '正常',
                        name: 'status',
                        id: 'status_0',
                        inputValue: '0',
                        checked: true
                    }, {
                        boxLabel: '禁止',
                        name: 'status',
                        id: 'status_1',
                        inputValue: '1'
                    }
                ]
            },{
                xtype: 'radiogroup',
                fieldLabel: '应用类型',
                vertical: true,
                id:'app_type',
                items: [
                    {
                        boxLabel: '普通应用',
                        name: 'app_type',
                        id: 'app_type_0',
                        checked: true,
                        inputValue: '0'
                    }, {
                        boxLabel: '管理应用',
                        name: 'app_type',
                        id: 'app_type_1',
                        inputValue: '1'
                    }
                ]
            },{
                xtype: 'radiogroup',
                fieldLabel: '验证类型',
                vertical: true,
                id:'auth_type',
                items: [
                    {
                        boxLabel: '无需验证',
                        name: 'auth_type',
                        id: 'auth_type_0',
                        checked: true,
                        inputValue: '0'
                    }, {
                        boxLabel: '口令验证',
                        name: 'auth_type',
                        id: 'auth_type_1',
                        inputValue: '1'
                    }
                ]
            },{
                xtype: 'textfield',
                name: 'access_key',
                fieldLabel: '访问口令',
                maxLength: 64
            },{
                xtype: 'textarea',
                fieldLabel: '应用信息',
                name: 'meta',
                maxLength: 200
            }
        ],
        buttons:[{ 
            text:'保  存',
            icon:'../../img/accept.png',
            handler:function(){
                if(!form.getForm().isValid()){
                     Ext.Msg.alert('信息提示','请检查输入项是否按要求填写！');
                     return;
                }
                if(Ext.String.trim(form.form.findField('app_name').getValue()).length == 0){
                    Ext.Msg.alert('信息提示','请输入应用名称！',function(){
                        form.form.findField('app_name').setValue('');
                        form.form.findField('app_name').focus();
                    });
                    return;
                }
                if(Ext.String.trim(form.form.findField('app_id').getValue()).length == 0){
                    Ext.Msg.alert('信息提示','请输入应用ID/编码！',function(){
                        form.form.findField('app_id').setValue('');
                        form.form.findField('app_id').focus();
                    });
                    return;
                }
                var auth_type = form.form.findField('auth_type').getValue().auth_type;
                if(auth_type == '1') {
                    //选择口令验证时，必须输入口令
                    var access_key = form.form.findField('access_key').getValue();
                    if(Ext.String.trim(access_key).length == 0) {
                        Ext.Msg.alert('信息提示','请输入访问口令！',function(){
                            form.form.findField('access_key').focus();
                        });
                        return;
                    }
                }
                form.form.submit({
                    waitMsg:'正在保存数据...',
                    success:function(form,action){
                        if(action.result.success){
                            Ext.Msg.alert('信息提示',action.result.msg,function(){
                                store.reload();
                                Ext.getCmp('myForm').getForm().reset();
                                addWin.close();
                            });
                        } else {
                            Ext.Msg.alert('信息提示',action.result.msg);
                        }
                    },
                    failure:function(form,action){
                        var result = Ext.decode(action.response.responseText);
                        if(!result.success){
                            Ext.Msg.alert('信息提示', result.msg);
                        } else {
                            Ext.Msg.alert('信息提示','操作失败，请重试！');
                        }
                    }
                });
            }
        },{
            text:'关  闭',
            icon:'../../img/close.png',
            handler:function(){
                this.up('form').getForm().reset();
                addWin.close();
            }
        }]
    });
    
    function updHandler(){
        var records = grid.getSelectionModel().getSelection();
        if(records.length > 1){
            Ext.Msg.alert("信息提示","只能选择一条数据进行修改！");
            return;
        } 
        if(records.length == 0){
            Ext.Msg.alert("信息提示","请选择需要修改的数据！");
            return;
        }
        form.form.reset();
        if(!addWin){
            addWin = Ext.create('Ext.Window', {
                title: '修改应用信息',
                width: 525,
                height: 355,
                constrainHeader: true,
                closeAction:'hide',
                modal: true,
                layout: 'fit',
                items: form
            });
        }
        form.getForm().url='/apps/edit';
        Ext.Ajax.request({
            url: '/apps/detail',
            params : {'id' : records[0].get('id')},
            method : 'get',
            success: function(response, opts) {
                var jsonResult = Ext.decode(response.responseText); 
                if(jsonResult.success == false){
                    return alert(jsonResult.error || jsonResult.msg);
                }
                if(!jsonResult){
                    return ;
                }
                for(var i in jsonResult){
                    if(form.form.findField(i)){
                        form.form.findField(i).setValue(jsonResult[i]);
                    }
                }
                //设置单选框
                if(jsonResult.app_type) {
                    Ext.getCmp('app_type').getComponent('app_type_'+jsonResult.app_type).setValue(true);
                }
                if(jsonResult.auth_type) {
                    Ext.getCmp('auth_type').getComponent('auth_type_'+jsonResult.auth_type).setValue(true);
                }
                if(jsonResult.status) {
                    Ext.getCmp('status').getComponent('status_'+jsonResult.status).setValue(true);
                }
                addWin.show();
            }
        });
    }
    
    function addHandler(){
        if(!addWin){
            addWin = Ext.create('Ext.Window', {
                title: '添加应用信息',
                width: 525,
                height: 355,
                bodyPadding: '0 0 0',
                modal: true,
                'z-index': 9999999,
                constrainHeader: true,
                layout: 'fit',
                closeAction:'hide',
                items: form
            });
        }
        form.getForm().url='/apps/add';
        addWin.show();
    }
    
    function delHandler(){
        var records = grid.getSelectionModel().getSelection();
        if(records.length == 0){
            Ext.Msg.alert("信息提示","请选择需要删除的数据！");
            return;
        }
        if(records.length > 1){
            Ext.Msg.alert("信息提示","不允许批量删除！");
            return;
        }
        Ext.MessageBox.confirm('信息提示','确认删除吗？',function(val){
            if(val == 'yes'){
                Ext.Ajax.request({
                    url: '/apps/del?id='+records[0].get("id")+'&app_id='+records[0].get('app_id'),
                    method : 'POST',
                    success: function(response, opts) {
                        var data = eval("("+response.responseText+")");
                        Ext.Msg.alert('信息提示',data.msg);
                        store.reload();
                    },
                    failure: function(response, opts) {
                        var data = eval("("+response.responseText+")");
                        if(!data.success){
                            Ext.Msg.alert('信息提示',data.msg);
                        } else {
                            Ext.Msg.alert("信息提示","系统忙，请稍候再试");
                        }
                    }
                });
            }
        });
    }
});
</script>
<body>
</body>
</html>